package net.zjitc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2017/10/12.
 */
@WebServlet(name = "AddServlet" ,urlPatterns = "/addServlet")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           //从请求里面拿参数
            String p1 = request.getParameter("num1");
            String p2 = request.getParameter("num2");

            // Strint -->int
          int num1;
          int num2;
        try {
             num1 = Integer.valueOf(p1);
             num2 = Integer.valueOf(p2);
        }catch (Exception e){
            //如果出现异常，我把异常的提示信息转发到form页面
             request.setAttribute("msg"," ，请重新输入");
             request.getRequestDispatcher("/form.jsp").forward(request,response);
             return;
        }


           //相加
        int sum = num1+num2;
        //设置域值
        request.setAttribute("sum" ,sum);
        request.getRequestDispatcher("/result.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
